package com.ruoyi.web.controller.mdm;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mdm.domain.*;
import com.ruoyi.mdm.mapper.SkuSampleMapper;
import com.ruoyi.mdm.service.ISkuSampleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * sku 小样管理Controller
 *
 * @author shiseido
 * @date 2025-03-01
 */
@RestController
@RequestMapping("/mdm/skuSample")
public class SkuSampleController extends BaseController {
    @Autowired
    private ISkuSampleService skuSampleService;

    @Autowired
    private SkuSampleMapper skuSampleMapper;

    /**
     * 查询sku小样管理列表
     */
    @ApiOperation("小样价格计算")
    @GetMapping("/calPrice/{ownerId}")
    public void calPrice(@PathVariable("ownerId") String ownerId) {
//        String operName = "admin";
        String operName = getUsername();
        skuSampleService.skuSampleCalPrice(ownerId, operName);
        return;
    }

    /**
     * 查询sku小样管理列表
     */
    @ApiOperation("查询sku小样管理列表")
    @GetMapping("/list")
    public TableDataInfo list(SkuSampleEntity SkuSampleEntity) {
        startPage();
        List<SkuSampleEntity> retList = new ArrayList<>();
        List<SkuSampleEntity> skuSampleEntityList = skuSampleService.selectSkuSampleList(SkuSampleEntity);
//        for (SkuSampleEntity entity : skuSampleEntityList){
//            SkuSamplePriceEntity skuSamplePrice = new SkuSamplePriceEntity();
//            if (null != entity.getSpuCode()){
//                skuSamplePrice.setOwnerId(entity.getOwnerId());
//                skuSamplePrice.setSpuCode(entity.getSpuCode());
//                SkuSamplePriceEntity retBean = skuSampleService.getSkuMaxMinPrice(skuSamplePrice);
//                if (null != retBean){
//                    entity.setMaxPrice(Double.parseDouble(retBean.getMaxPrice()));
//                    entity.setMinPrice(Double.parseDouble(retBean.getMinPrice()));
//                }
//            }
//
//            retList.add(entity);
//        }

        return getDataTable(skuSampleEntityList);
    }

    /**
     * 查询sku小样管理列表
     */
    @ApiOperation("查询指定sku小样")
    @GetMapping("/getOneSku")
    public TableDataInfo getOneSku(SkuSampleEntity SkuSampleEntity) {
        startPage();
        List<SkuSampleEntity> list = skuSampleService.getOneSku(SkuSampleEntity);
        return getDataTable(list);
    }

    /**
     * 查看小样详情
     */
    @ApiOperation("查看小样基本信息")
    @GetMapping("/skuInfo")
    public TableDataInfo getInfo(SkuSampleEntity skuSampleEntity) {
        startPage();

        List<SkuSampleEntity> list = new ArrayList<>();
        list.add(skuSampleService.selectSkuSampleById(skuSampleEntity.getId()));
        return getDataTable(list);
    }

    @ApiOperation("查看小样修订记录")
    @GetMapping("/skuUpdateLog")
    public TableDataInfo skuUpdateLog(SkuSampleUpdateLog skuSampleEntity){
        startPage();
        List<SkuSampleUpdateLog> list = skuSampleService.getSkuSampleUpdateLog(skuSampleEntity);
        return getDataTable(list);
    }

    /**
     * 导入产品属性
     */
    @ApiOperation("导入sku小样属性")
    @Log(title = "SKU小样管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {

        ExcelUtil<SkuSampleImport> util1 = new ExcelUtil<SkuSampleImport>(SkuSampleImport.class);
        List<SkuSampleImport> skuSampleSubImportList = util1.importExcel(
                "小样容量和关联SPU", file.getInputStream(), 0);


        String operName = getUsername();

        skuSampleService.importSkuSampleSub(skuSampleSubImportList,
                operName);

        return success();
    }

    /**
     * 修改sku小样管理
     */
    @ApiOperation("编辑sku小样管理")
    @Log(title = "sku小样管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SkuSampleMasterSub skuSampleMasterSub) {
        skuSampleMasterSub.setCreateBy(getUsername());
        skuSampleMasterSub.setUpdateBy(getUsername());
        return toAjax(skuSampleService.editSkuSampleSub(skuSampleMasterSub));
    }

    /**
     * 小样计算异常提醒
     */
    @ApiOperation("小样计算异常提醒")
    @GetMapping("/getSkuSamplePriceCalErrorLog")
    public TableDataInfo getSkuSamplePriceCalErrorLog(SkuSamplePriceCalErrorEntity SkuSampleEntity) {
        startPage();
        List<SkuSamplePriceCalErrorEntity> list = skuSampleService.getSkuSamplePriceCalErrorLog(SkuSampleEntity);
        return getDataTable(list);
    }

    @ApiOperation("查看小样价值")
    @GetMapping("/skuValue")
    public TableDataInfo skuValue(SkuSamplePriceEntity skuSampleEntity){
        startPage();
        skuSampleEntity.setSampleSkuCode(skuSampleEntity.getGoodsCode());
        List<SkuSamplePriceEntity> list = skuSampleService.selectSkuSampleValue(skuSampleEntity);
        return getDataTable(list);
    }

    /**
     * 修订历史追溯
     */
    @ApiOperation("修订历史追溯")
    @GetMapping("/getSkuSampleUpdateLog")
    public TableDataInfo getSkuSampleUpdateLog(SkuSampleUpdateLog SkuSampleEntity) {
        startPage();
        List<SkuSampleUpdateLog> list = skuSampleService.getSkuSampleUpdateLog(SkuSampleEntity);
        return getDataTable(list);
    }


}
